﻿//
//  NewForumViewController.m
//  WDFans
//
//  Created by Zhihong Yuan on 2/28/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "NewForumViewController.h"
#import "ForumListTableCell.h"
#import "ThemesListViewController.h"
#import "ZHDataService.h"
#import "ZHWebService.h"
#import "WDForum.h"
#import "WDForumSection.h"
#import "UIImageView+LoadImagesFromInternet.h"

@interface NewForumViewController()

@property (nonatomic, retain) NSArray *allSections;

@end

@implementation NewForumViewController

@synthesize allSections;

#pragma mark - my methods
- (void)getForumSections
{
    CompletionBlock completion = ^(NSData *data){
        NSMutableArray *sections = [ZHDataService parseToForumSectionsWithData:data];
        self.allSections = sections;
        [self.myTableView reloadData];
    };
    ErrorBlock error = ^(NSString *error){};
    [[ZHWebService sharedService] getForumSectionsOnCompletion:completion onError:error];
}

#pragma mark - View lifecycle

- (void)dealloc
{
    [allSections release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"论坛";
    [self setBackgroundImageViewForTabView];
    
    [self getForumSections];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}
#pragma mark - UITableViewDatasource methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kHeightOfFroumListTableCell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    WDForumSection *s = [self.allSections objectAtIndex:section];
    return s.name;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [self.allSections count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    WDForumSection *s = [self.allSections objectAtIndex:section];
    return [s.forums count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"CellIdentifier";
    ForumListTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"ForumListTableCell" owner:self options:nil];
        cell = (ForumListTableCell *)[nibs lastObject];
    }
    WDForumSection *s = [self.allSections objectAtIndex:indexPath.section];
    WDForum *f = [s.forums objectAtIndex:indexPath.row];
    cell.nameLabel.text = f.name;
    cell.desLabel.text = f.description;
    cell.postsLabel.text = [NSString stringWithFormat:@"%d",f.posts];
    
//    NSLog(@"f.icon=%@",f.icon);
    
    [cell.iconImageView loadImageAsynchronousWithURL:f.icon placeHolderImage:@"defaultForum.png"];
    return cell;
}

#pragma mark - UITableViewDelegate methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ThemesListViewController *themesListController = [[ThemesListViewController alloc] init];
    WDForumSection *fs = [self.allSections objectAtIndex:indexPath.section];
    WDForum *f = [fs.forums objectAtIndex:indexPath.row];
    themesListController.forum = f;
    [self.navigationController pushViewController:themesListController animated:YES];
    [themesListController release];
}

#pragma mark -
#pragma mark Data Source Loading / Reloading Methods

- (void)reloadTableViewDataSource{
    [super reloadTableViewDataSource];
    [self getForumSections];
}
@end
